<template>
  <div class="TMB-warp">
    <div class="titler border-bottom">
      <span class="font-w fs16">{{ $t("message.plan.Hand.title") }}</span>
      <i class="el-icon-close" @click="CloseTables"></i>
    </div>
    <div class="middle-box">
      <div class="left-box border-right">
        <div class="row font-w border-bottom fs16">
          {{ $t("message.plan.Hand.label_1") }}
        </div>
        <div class="row">
          <span class=" font-w mar8">{{ $t("message.plan.Hand.label_2") }}</span>
          <span class="gray">
            {{ $store.state.plantName }}
          </span>
        </div>
        <div class="row">
          <span class=" font-w mar8">{{ $t("message.plan.Hand.label_4") }}</span> <span class="mar8 gray">{{
            $store.state.macType == 1 ? $t("message.plan.TBM.label_5") : '配管' }}</span><el-input class="mar8" readonly
            v-model="macNum"></el-input>
          <span>{{ $t("message.plan.Hand.label_6") }}</span>
        </div>
      </div>
      <div class="right-box">
        <div class="row font-w border-bottom fs16">
          {{ $t("message.plan.Hand.label_7") }}
        </div>

        <div class="row">
          <span style="margin-right:15px;">{{ $t("message.plan.Hand.label_8") }}</span>
          <el-date-picker v-model="time" type="month" class="ipt" placeholder=" " :clearable="false"
            :format="timeFormat" :value-format="timeFormat" @change="dateChange" :picker-options="pickerOptions">
          </el-date-picker>
        </div>
      </div>
    </div>
    <div class="footer border-top">
      <span class="font-w">{{ $t("message.plan.Hand.label_9") }}</span>
      <div>
        <el-button @click="CloseTables">Cancel</el-button>
        <el-button type="primary" @click="_addBtn">OK</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import BASE from '@/utils/base'
import { GetManualPlanNextDate, AddManualPlanBefore, AddManualPlan, visualDetailManual } from '@/api/api'
import eventBus from "@/eventBus/bus";
import { getLocalStorage } from "@/utils/storage";

export default {
  name: "HANPlan",
  props: {
    selectData: {
      type: Array
    },
  },
  data() {
    return {
      test: false,
      time: "2022-02",
      timeFormat: "yyyy-MM",
      macNum: 0,
      pickerOptions: {
        disabledDate(time) {
          // console.log('time.getTime()', time.getTime());
          return time.getTime() < new Date().getTime();
        }
      },

    };
  },
  components: {},
  watch: {},
  methods: {
    async _addBtn() {
      //预先执行
      let dataJson = {}
      let ChildList = [];
      for (let i = 0; i < this.selectData.length; i++) {
        let t = {
          PlanNo: Number(this.selectData[i].PLANNO),
          dmgCode: Number(this.selectData[0].DMGCODE),
        }
        ChildList.push(t)
      }
      dataJson = {
        PltCode: this.$store.state.pltCode == "" ? 0 : this.$store.state.pltCode,
        ChildList,
        NextDate: this.time,
      }
      let res = await visualDetailManual(dataJson);
      if (res.data.code != 0) {
        this.$alert(res.data.msg, '', {
          confirmButtonText: 'OK',
          type: 'warning',
        });

        return
      }
      this.$message({
        type: "success",
        message: res.data.msg
      })
      this.$emit('CloseTables')
      this.$emit("reGetMSTableInfo");
    },
    //修改预先执行
    async AddManualPlanBeforeApi(dataJson, index) {

      let res = await AddManualPlanBefore(dataJson);		//1确认2取消
      console.log(res)
      if (res.data.code != 0) {
        this.selectData[index].flg = 1;
        let isAdd = true;
        this.selectData.forEach((i, v) => {
          if (!i.flg) {
            console.log('有不存在')
            isAdd = false
          }
        })
        if (isAdd) {
          console.log('执行新增')
          this.AddManualPlanApi();
          return
        }
        return
      }

      this.$confirm(res.data.data, '', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        this.selectData[index].flg = 1;
        let isAdd = true;
        this.selectData.forEach((i, v) => {
          if (!i.flg) {
            console.log('有不存在')
            isAdd = false
          }
        })
        if (isAdd) {
          console.log('执行新增')
          this.AddManualPlanApi();
        }
      }).catch(() => {
        this.selectData[index].flg = 2;
        let isAdd = true;
        this.selectData.forEach((i, v) => {
          if (!i.flg) {
            console.log('有不存在')
            isAdd = false
          }
        })
        if (isAdd) {
          console.log('执行新增')
          this.AddManualPlanApi();
        }
      });
    },
    //执行新增
    async AddManualPlanApi() {
      let Arr = [];
      for (let i = 0; i < this.selectData.length; i++) {
        if (this.selectData[i].flg == 1) {
          let dataJson = {
            updUser: "admin",
            dmgCode: Number(this.selectData[i].DMGCODE),
            macNumb: this.selectData[i].MACNUMB,
            wrkClsCode: Number(this.selectData[i].WRK_CLS_CODE),
            nextDate: this.time,
          }
          Arr.push(dataJson);
        }
      }
      let res = await AddManualPlan(Arr);
      console.log(res)
      this.$message({
        type: "success",
        message: res.data.data
      })
      this.$emit('CloseTables')
      this.$emit('reGetMSTableInfo')
    },
    //获取次回日期
    async GetManualPlanNextDateApi() {
      let res = await GetManualPlanNextDate({});
      console.log(res, "次回");
      if (res.data.code != 0) {
        return
      }
      this.time = res.data.data;
    },

    CloseTables() {
      this.$emit('CloseTables')
    },
    dateChange(val) {
      console.log(`dateChange(${this.dateValue})`, val);
      //     this.getResOfferEjectDataApi();
    },
  },
  created() { },
  mounted() {
    this.macNum = this.selectData.length;
    this.GetManualPlanNextDateApi();
    console.log(GetManualPlanNextDate);
    if (BASE.IP == "http://192.168.33.200:8085") {
      this.timeFormat = "yyyy-MM";
    } else if (BASE.IP == "https://eng-dev-api.a-mis.com") {
      this.timeFormat = "MM-yyyy";
    } else if (BASE.IP == "https://demoapi.richtechcn.com") {
      this.timeFormat = "yyyy-MM";
    }
  },
  beforeDestroy() { },
  destroyed() { }
};
</script>
<style lang="scss" scoped>
.TMB-warp {
  width: 800px;
  height: 240px;
  background: rgb(255, 255, 255);
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
    0px 9px 28px 0px rgba(0, 0, 0, 0.08), 0px 6px 16px -8px rgba(0, 0, 0, 0.15);
  z-index: 100;
}

.titler {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 24px;

  i {
    cursor: pointer;
  }
}

.middle-box {
  height: calc(100% - 100px);
  display: flex;

  .left-box {
    width: 281px;
    padding: 0 24px;
  }

  .right-box {
    width: calc(100% - 281px);
    padding: 0 24px;
  }

  .row {
    padding: 10px 0;
    display: flex;
    align-items: center;

    .el-input {
      width: 70px;
      height: 32px;

      ::v-deep .el-input__inner {
        width: 70px;
        height: 100%;
        text-align: center;
      }
    }

    ::v-deep .el-radio__label {
      font-weight: bold;

    }

    .ipt {
      width: 120px !important;
      height: 32px;
      margin-right: 8px;

      ::v-deep .el-input__inner {
        width: 100%;
        height: 32px;
        padding: 0 8px 0 30px;
      }

      // ::v-deep .el-input__inner:focus {
      //   border: 1px solid #dcdfe6;
      // }
      ::v-deep .el-input__icon {
        line-height: 32px;
        color: #909399;
      }

      ::v-deep .el-input__suffix {
        display: none;
      }
    }
  }
}

.footer {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 24px;

  button {
    width: 82px !important;
    height: 32px;
    // background: #ffffff;
    // border: 1px solid #d9d9d9;
    // cursor: pointer;
    line-height: 0;
  }

  button:nth-child(2) {
    margin-left: 16px;
    // color: #ffffff;
    // background: #106ebe;
  }
}

.font-w {
  font-weight: bold;
}

.border-top {
  border-top: 1px solid #f0f0f0;
}

.border-bottom {
  border-bottom: 1px solid #f0f0f0;
}

.border-left {
  border-left: 1px solid #f0f0f0;
}

.border-right {
  border-right: 1px solid #f0f0f0;
}

.gray {
  color: #7B7B7B;
}

.mar8 {
  margin-right: 8px;
}

.fs16 {
  font-size: 16px;
}
</style>
